package com.psbc.online_xdclass.service.impl;

import com.psbc.online_xdclass.exception.XDException;
import com.psbc.online_xdclass.mapper.EpisodeMapper;
import com.psbc.online_xdclass.mapper.RecordMapper;
import com.psbc.online_xdclass.mapper.VideoMapper;
import com.psbc.online_xdclass.mapper.VideoOrderMapper;
import com.psbc.online_xdclass.model.entity.Episode;
import com.psbc.online_xdclass.model.entity.PlayRecord;
import com.psbc.online_xdclass.model.entity.Video;
import com.psbc.online_xdclass.model.entity.VideoOrder;
import com.psbc.online_xdclass.service.VideoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class VideoOrderServiceImpl implements VideoOrderService {
    @Autowired
    private VideoOrderMapper videoOrderMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private EpisodeMapper episodeMapper;
    @Autowired
    private RecordMapper recordMapper;
    /**
     * 下单接口
     * @param userId
     * @param videoId
     * @return
     */
    @Override
    @Transactional
    public int save(int userId, int videoId) {
        VideoOrder videoOrder = videoOrderMapper.findByUserIdAndVideoIdAndState(userId,videoId,1);
        if (videoOrder != null){
            return 0;//说明订单已经存在，直接返回
        }
        //订单不存在，开始创建订单，但是订单接口里有很多视频的字段，所以还要查询出视频的详细信息
        Video video = videoMapper.findVideoById(videoId);

        VideoOrder newVideoOrder = new VideoOrder();
        newVideoOrder.setCreateTime(new Date());
        newVideoOrder.setState(1);
        newVideoOrder.setOutTradeNo(UUID.randomUUID().toString());
        newVideoOrder.setTotalFee(video.getPrice());
        newVideoOrder.setVideoId(videoId);
        newVideoOrder.setVideoImg(video.getCoverImg());
        newVideoOrder.setVideoTitle(video.getTitle());
        newVideoOrder.setUserId(userId);
        int rows = videoOrderMapper.saveOrder(newVideoOrder);
        //生成播放记录
        if (rows == 1){
            Episode episode = episodeMapper.findFirstEpisodeByVideoId(videoId);
            if (episode == null){
                throw new XDException(-1,"视频没有集信息，请程序员检查");
            }
            PlayRecord playRecord = new PlayRecord();
            playRecord.setCreateTime(new Date());
            playRecord.setUserId(userId);
            playRecord.setVideoId(videoId);
            playRecord.setEpisodeId(episode.getId());
            playRecord.setCurrentNum(episode.getNum());//当前播放的是第几集
            recordMapper.saveRecord(playRecord);
        }
        return rows;

    }

    /**
     * 根据用户id查询订单列表接口
     * @param userId
     * @return
     */
    @Override
    public List<VideoOrder> findOrderByUserId(int userId) {
        List<VideoOrder> videoOrderList = videoOrderMapper.findOrderByUserId(userId);
        return videoOrderList;
    }
}
